import Food from "./food.js";
import Snake from "./snake.js";
import Direction from "../lib/direction.js";
import Config from "../config/config.js";

// 定义一个游戏对象，通过指挥游戏对象完成工作
export default class Game {
    constructor(opt) {
        this.food = new Food();
        this.food.init();
        this.food.render(opt.parent);
        this.snake = new Snake();
        this.map = opt.parent;
        this.snake.render(opt.parent);
        this.interval = Config.SnakeInterval || 250;
        this.onEnd = null;
    }
    start() {
        this.keyControl();

        this.food.autoRun(this.map);

        // 设定定时器来移动蛇
        this.timer = setInterval(function () {
            // 通这里的判断，处理下一个位置，判断下一个位置是否超过边界，处理蛇头不会超出边界
            let head = this.snake.body[0];
            let nextPos = {};
            // 把蛇头里面的每一个键值对复制到nextPos里面
            nextPos = Object.assign(nextPos, head);
            // 根据方向算出蛇头下一步到哪里
            switch (this.snake.direction) {
                case Direction.TOP:
                    nextPos.y -= head.height;
                    break;
                case Direction.RIGHT:
                    nextPos.x += head.width;
                    break;
                case Direction.BOTTOM:
                    nextPos.y += head.height;
                    break;
                case Direction.LEFT:
                    nextPos.x -= head.width;
                    break;
            }
            // 判断蛇头下一步是否到达边界
            if (nextPos.x < 0 || nextPos.x > this.map.clientWidth - nextPos.width || nextPos.y < 0 || nextPos.y > this.map.clientHeight - nextPos.height) {

                // 到达边界之后清除定时器
                clearInterval(this.timer);
                clearInterval(this.food.moveTimer);
                clearInterval(this.food.dirTimer);
                //调用自己的onEnd
                if(typeof this.onEnd === 'function'){
                    this.onEnd();
                }

                return;
            }
            // 调用蛇移动的方法
            this.snake.move();

            // 食物碰撞
            if (head.x == this.food.x && head.y == this.food.y) {
                this.food.remove(this.map);
                this.snake.growth(this.map);
                this.food = new Food();
                this.food.init();
                this.food.render(this.map);
                this.food.autoRun(this.map);
            }
        }.bind(this), this.interval);
    }
    keyControl() {
        let _that = this;
        document.addEventListener('keydown', function (e) {
            switch (e.keyCode) {
                case 37:
                    _that.snake.direction = Direction.LEFT;
                    _that.snake.body[0].element.children[0].className = 'snakeHead left';
                    break;
                case 38:
                    _that.snake.direction = Direction.TOP;
                    _that.snake.body[0].element.children[0].className = 'snakeHead top';
                    break;
                case 39:
                    _that.snake.direction = Direction.RIGHT;
                    _that.snake.body[0].element.children[0].className = 'snakeHead right';
                    break;
                case 40:
                    _that.snake.direction = Direction.BOTTOM;
                    _that.snake.body[0].element.children[0].className = 'snakeHead bottom';
                    break;
            }
        });
    }
}